package com.ruoyi.web.controller.social;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.social.domain.SocialProduct;
import com.ruoyi.social.service.ISocialProductService;
import com.ruoyi.social.service.ISocialShopService;
import com.ruoyi.social.service.SocialProcessorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/social")
public class WebSocialController extends BaseController {
    @Autowired
    private ISocialProductService productService;

    @Autowired
    private ISocialShopService shopService;

    @Autowired
    SocialProcessorService socialProcessorService;

    @GetMapping("/list")
    public TableDataInfo list(SocialProduct product) {
        startPage();
        List<SocialProduct> listView = productService.selectSocialProductList( product, SecurityUtils.getUserId() );
        return getDataTable(listView);
    }

    @PostMapping("/sync-flow")
    public AjaxResult syncAll(@RequestBody JSONObject jsonObject) {
        String url = jsonObject.getString("url");
        String channel = jsonObject.getString("channel"); // shoplus  fastmoss
        String type = jsonObject.getString("type"); // 类型 商品 product store
        if (StringUtils.isEmpty(url)) {
            return error(MessageUtils.message("product.no.url"));
        }
        if (Objects.equals(type, "product")) {
            productService.processorProductStart(url, channel);
        } else if (Objects.equals(type, "store")) {
            shopService.processorShopStart(url, channel);
        } else {
            return error(MessageUtils.message("product.no.type"));
        }
        return success();
    }

    @PostMapping("/sync-end/{id}/{userId}")
    public AjaxResult syncAllEnd(@PathVariable("id") Long id,@PathVariable("userId") Long userId, @RequestBody JSONObject jsonObject) {
        productService.processorProductEnd(id,userId, jsonObject);
        return success();
    }
}
